
import com.etop.weibo.beans.User;
import com.etop.weibo.dao.DAOFactory;
import com.etop.weibo.dao.UserDao;
import com.etop.weibo.util.HibernateUtil;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.hibernate.Hibernate;
import org.hibernate.Session;
import org.junit.Test;

/**
 *
 * @author jansing
 */
public class UserCRUD {

    @Test
    public void testupdate(){
        User user = new User();
        user.setId(27);
        user.setNickname("wowow");
        user.setUsername("ki89ol");
        DAOFactory.getUserDao().update(user);
    }
    
    public void basedao03(){
        UserDao ud = new UserDao();
        List<String> params = new ArrayList<>();
        params.add("username");
        params.add("nickname");
        Map<String, Object> condition = new HashMap<>();
        condition.put(UserDao.USERNAME, "hhhh");
        Object[] objs = ud.loadByHQL(params, condition);
        for(Object obj : objs){
            System.out.println(obj);
        }
    }
    
    
    public void basedao02(){
        UserDao ud = new UserDao();
        Map<String, Object> params = new HashMap<>();
        params.put(UserDao.USERNAME, "hhhh");
        User user = ud.loadByHQL(params);
        System.out.println(user);
    }
    
    public void basedao01(){
        UserDao ud = new UserDao();
        Map<String, Object> params = new HashMap<>();
        params.put(UserDao.USERNAME, "JWeb");
//        ud.deleteByHQL(null, params);
    }
    
    public void list01(){
        List<User> list = DAOFactory.getUserDao().list();
        for(User user : list){
            System.out.println(user.getUsername() + "   " + user.getPassword());
        }
    }
    
    public void update01(){
        User user = DAOFactory.getUserDao().load(25);
        user.setUsername("hhhh");
        DAOFactory.getUserDao().update(user);
    }
    
    public void load01(){
        String hql = "from User where id=1";
        Session session = HibernateUtil.openSession();
        User user = (User) session.createQuery(hql).uniqueResult();
        System.out.println(user);
    }
    
    public void delete02(){
        Session session = null;
        
        session = HibernateUtil.openSession();
        session.beginTransaction();
        session.createQuery("delete from User where id=22").executeUpdate();
        session.getTransaction().commit();
        session.close();
    }
    
    public void delete01(){
        DAOFactory.getUserDao().delete(23);
    }
    
    public void testAdd(){
        User user = new User();
        System.out.println(user.getId());
        user.setNickname("荧惑");
        user.setUsername("JWeb");
        user.setPassword("5054");
        user.setIntroduction("769");
        DAOFactory.getUserDao().add(user);
        System.out.println(user.getId());
    }
    
    public void testAdd01() {
        Session session = null;
        try {
            session = HibernateUtil.openSession();
            session.getTransaction().begin();
            for(int i= 0; i< 100; i++){
            User user = new User();
            user.setUsername("jansing"+i);
            user.setPassword("jasning"+i);
            user.setSex("男");
            session.save(user);
            }
            session.getTransaction().commit();
        } catch (Exception e) {
            if (session != null) {
                session.getTransaction().rollback();
            }
        } finally {
            if (session != null) {
                session.close();
            }
        }
    }
}
